#include "DialogManager.hpp"
#include "../RenderingSystem/DialogFontAnimated.hpp"
#include "GuiBox.hpp"
#include "GuiDialog.hpp"
#include "GuiChoice.hpp"
#include "../Core.hpp"
#include "../ChatSystem/CommandManager.hpp"
#include "../ChatSystem/Dialog.hpp"


DialogManager::DialogManager(Core *core, float txtSpeed)
{
    m_core = core;
    textSpeed =txtSpeed;
    commandManager = new CommandManager(core->itemManager, this);
    guiDialog = new GuiDialog(this, txtSpeed);
    guiChoice = new GuiChoice(this, core);
    currentDialog = NULL;

}

DialogManager::~DialogManager()
{
    delete commandManager;
    delete guiDialog;
    delete guiChoice;
}

void DialogManager::draw(sf::RenderWindow *window)
{
    guiDialog->draw(window);
    guiChoice->draw(window);
}

void DialogManager::update()
{
    guiDialog->update();
    guiChoice->update();
}

void DialogManager::handleInput(sf::Event *event)
{
    if (event->type == sf::Event::KeyPressed)
    {
        switch (event->key.code)
        {
            case sf::Keyboard::Space:
                if(!guiChoice->isActive())
                    guiDialog->spaceInput();
                break;
            case sf::Keyboard::Return:
                if(guiChoice->isActive())
                {
                    guiChoice->returnInput();
                    guiDialog->spaceInput();
                }
                break;
            case sf::Keyboard::Up:
                if(guiChoice->isActive())
                {
                    guiChoice->upDownInput();
                }
                break;
            case sf::Keyboard::Down:
                if(guiChoice->isActive())
                {
                    guiChoice->upDownInput();
                }
                break;
            case sf::Keyboard::F12:
                m_core->window.capture().saveToFile("Screenshots/screen1.png");
            break;
            case sf::Keyboard::F1:
                if(Core::debug)
                {
                    std::cout<<"Debug disabled"<<std::endl;
                    Core::debug = false;
                }
                else
                {
                    std::cout<<"Debug enabled"<<std::endl;
                    Core::debug = true;
                }
            break;
        }
    }
}

void DialogManager::newDialog(Dialog*dialog)
{
    currentDialog = dialog;
    enableDialog(dialog->getCurrentString());
}

void DialogManager::enableChoice(std::string yes, std::string no)
{
    guiChoice->newChoice(yes,no);
}

void DialogManager::disableChoice()
{

}

void DialogManager::enableDialog(std::string msg)
{
    guiDialog->addDialogChunk(msg);
    guiDialog->displayCurrentDialog();
}

void DialogManager::disableDialog()
{

}

bool DialogManager::isActiveOrWaiting()
{
    if (guiDialog->isActiveOrWaiting()||guiChoice->isActiveOrWaiting())
    {
        return true;
    }
    return false;
}
